<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@page import="common.DBUtil"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="news.Newsdao"%>
<%@page import="news.News"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻审核中心</title>
<link rel="stylesheet" type="text/css" href="./css/news_management.css"/>
<style type="text/css">
a{
text-decoration:none;
}
</style>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
<script type="text/javascript">
	window.onload = function color() {//窗口加载时调用  
		var table = document.getElementById("table");//根据table的 id 属性值获得对象  
		var rows = table.getElementsByTagName("tr");//获取table类型的tr元素的列表  
		for (var i = 0; i < rows.length; i++) {
			if (i % 2 == 0) {
				rows[i].style.backgroundColor = "red";//偶数行时背景色为#EAF2D3  
			} else {
				rows[i].style.backgroundColor = "green";//单数行时背景色为white  
			}
		}
	}
</script>
<script type="text/javascript">
 //批量删除
	function delall() {
		alert("确定删除吗？？");
		var chekId = [];//先定义一个空的数组	
		var chek=new String();
		if ($("input[type='checkbox']:checked").length > 0) {
			$("input[type='checkbox']:checked").each(function(i) {
				chekId[i] = $(this).val();//选中的框的值赋值给定义的数组
			})
			//把数组转换为字符串
			for(var i = 0;i<chekId.length;i++){
				chek = chek+chekId[i]+",";
			}
			//赋值完之后再传到后台
			window.location.href = "${pageContext.request.contextPath}/DeleteNewsServlet?ckekId="
					+ chek;
		} else {
			alert("请选择你要删除的信息");
		}
	}
 //全选
 function qx(){
	var check1 = document.getElementsByName("check");
	for(var x =0;x <check1.length;x++){
		check1[x].checked=true;
		}
	
}
//反选
function fx(){
	var ck = document.getElementsByName("check");
	for(var x =0;x < ck.length;x++){
		if(ck[x].checked){
			ck[x].checked=false;
		}else{
			ck[x].checked=true;
		}
	}
}
</script>
<script type="text/javascript">
	function reallyPow(uuu) {
		var hint = "确认审核";
		var jg = confirm(hint);
		if (jg) {
			return true;
		}
		return false;
	}
</script>
</head>
<body style="background-image: url(../images/1.png);">
	<table id="table" border="1" align="center" width="80%" cellspacing="1"
		cellpading="1">
		<h1>新闻审核中心</h1>
		<tr>
		    <td>选择</td>
			<td style="text-align: center;">新闻编号</td>
			<td >新闻标题</td>
			<td style="text-align: center;">发布人</td>
			<td style="text-align: center;">发布时间</td>
			<td style="text-align: center;">是否置顶</td>
			<td style="text-align: center;">审核状态</td>
			<td style="text-align: center;">操作</td>
		<tr>
			<%
			int pages=0;            //待显示页面
		    int count=0;            //总条数
		    int totalpages=0;        //总页数
		    int limit=5;            //每页显示记录条数    
		    //计算记录总数的第二种办法：使用mysql的聚集函数count(*)
		     DBUtil db = new DBUtil();
		    ResultSet sqlRst = db.executeQuery("select count(*) from news");
		    if(sqlRst.next()){
		        count = sqlRst.getInt(1);//结果为count(*)表，只有一列。这里通过列的下标索引（1）来获取值
		    }    
		    //由记录总数除以每页记录数得出总页数
		    totalpages = (int)Math.ceil(count/(limit*1.0));
		    //获取跳页时传进来的当前页面参数
		    String strPage = request.getParameter("pages");
		    //判断当前页面参数的合法性并处理非法页号（为空则显示第一页，小于0则显示第一页，大于总页数则显示最后一页）
		    if (strPage == null) { 
		        pages = 1;
		    } else {
		        try{
		            pages = java.lang.Integer.parseInt(strPage);
		        }catch(Exception e){
		            pages = 1;
		        }
		        
		        if (pages < 1){
		            pages = 1;
		        }
		        
		        if (pages > totalpages){
		            pages = totalpages;
		        }                            
		    }
		    //由(pages-1)*limit算出当前页面第一条记录，由limit查询limit条记录。则得出当前页面的记录
		    Newsdao newsdao = new Newsdao();
		    ArrayList<News> list = newsdao.doNewsPage(pages, limit);
		    Iterator<News> iterator = list.iterator();
		    while (iterator.hasNext()){//遍历显示}
					News news = iterator.next();
					int isValib = news.getIsValid();
					int isTop= news.getIsTop();
					String istop;
					if(isTop == 1){
						istop ="置顶";
					}else{
						istop ="不置顶";
					}
					String isValib_s;
					if (isValib == 0) {
						isValib_s = "未审核";
					} else {
						isValib_s = "已审核";
					}
			%>
			<td class="f3">
			    <input type="checkbox" name="check" value="<%=news.getNid()%>">
			</td>
			<td class="f3"><%=news.getNid()%></td>
			<td class="show" title="${msgContent}"><%=news.getTitle()%></td>
			<td class="f2"><%=news.getUname()%></td>
			<td class="f1"><fmt:formatDate value="${news.getPubtime()}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
			<td class="f4"><%=istop%></td>
			<td class="f4"><%=isValib_s%></td>
			<td class="f5">
			<a  href="../news_management?nid=<%=news.getNid()%>" onclick="return reallyPow()">审核</a> 
			<a href="../ServletNewsManagement?nid=<%=news.getNid()%>">查看</a>
			<a href="">是否置顶</a>
			</td>
			
		</tr>
		<%
			}
		%>

	</table>
	<form name="f1" method="POST" action="news_management.jsp" onSubmit="return checknum()">
            <table border="0" align="center" >
                <tr>
                    <td><input type="button" value="全选" onclick="qx()" id="ck"></td>
                    <td><input type="button" value="反选" onclick="fx()" id="check"></td>
					<td><input type="button" value="批量删除" onclick="delall()"></td>
                    <td>第<%=pages%>页 共<%=totalpages%>页 <a href="news_management.jsp?pages=1">首页</a></td>
                    <td><a href="news_management.jsp?pages=<%=(pages<1)?pages:(pages-1) %>"> 上一页</a></td>
                    <td><a href="news_management.jsp?pages=<%=(pages>=totalpages)?totalpages:(pages+1)%>"> 下一页</a></td>
                    <td><a href="news_management.jsp?pages=<%=totalpages%>">最后一页</a></td>
                    <td>转到第:<input type="text" name="page" size="8">页<input type="submit" value="GO" name="cndok"></td>
                </tr>
            </table> 
 </form>
</body>
</html>